package com.typing.battle.view;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.typing.battle.R;
import com.typing.battle.activity.TypingBattleChatRoomActivity;
import com.typing.battle.activity.TypingBattleTimeAttackActivity;
import com.typing.battle.listener.OnDataSyncCompleteListener;
import com.typing.battle.util.PreferenceMgr;
import com.typing.battle.util.ServerComunicationUtil;
import com.typing.battle.value.ServerComunicationDTO;
import com.typing.battle.view.ChatRoomListView.ChatRoomListAdapter;

public class TimeAttackListView   extends ListView implements OnItemClickListener,OnDataSyncCompleteListener{

	private Context mContext;
	private Activity mActivity;
	public JSONArray list = new JSONArray();
	
	private TimeAttackListAdapter adapter;

	private LinearLayout layout;
	public TextView sentence;
	public TextView champion;
	private LayoutInflater inflate;
	
	private PreferenceMgr mgr;
	private int rowCount = 15;
	private int currentIndex = 0;
	private int totalCount;
	public TimeAttackListView(Context context) {
		super(context);
		mContext = context;
		mActivity = (Activity)context;
		inflate = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		adapter = new TimeAttackListAdapter();
//		getTimeAttackList();
		setAdapter(adapter);
		setOnItemClickListener(this);
		mgr = PreferenceMgr.getInstance(context);
	}
	public void getTimeAttackList(){
		ServerComunicationDTO dto = new ServerComunicationDTO();
		dto.setActionType(ServerComunicationUtil.TYPE_TIME_ATTACK_LIST);
		dto.setResponseType(ServerComunicationUtil.RETURN_TYPE_STRING);
		dto.setStartIndex(currentIndex);
		dto.setEndIndex(currentIndex + rowCount);
		try{
			ServerComunicationUtil.serverAction(dto,mContext,this);
		}catch(Exception e){
			Log.e("", "TYPE_TIME_ATTACK_LIST ERROR ::: "+e.getMessage());
			e.printStackTrace();
		}
	}
	public void refreshData(){	
		currentIndex = 0;
		list = new JSONArray();
		getTimeAttackList();
		adapter.notifyDataSetChanged();
	}
	public void getNextList(){
		getTimeAttackList();
		adapter.notifyDataSetChanged();
	}
	public class TimeAttackListAdapter extends BaseAdapter{
		
		@Override
		public int getCount() {
			return list == null ? 0 : list.length();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if(convertView ==null){
				layout  = (LinearLayout)inflate.inflate(R.layout.time_attack_list_row, null);
			}else{
				layout =(LinearLayout)convertView;	
			}
			sentence = (TextView)layout.findViewById(R.id.sentence);
			champion = (TextView)layout.findViewById(R.id.champion);
			
			try{
				JSONObject data = new JSONObject(list.get(position).toString());
				sentence.setText(data.get("sentence").toString());
				champion.setText(data.get("champion").toString());
			}catch(Exception e){
				
			}
			
			return layout;
		}
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		try{
			JSONObject data = new JSONObject(list.get(position).toString());
			Intent intent = new Intent(mContext,TypingBattleTimeAttackActivity.class);
			intent.putExtra("SENTENCE_ID", data.get("sentenceId").toString());
			intent.putExtra("SENTENCE", data.get("sentence").toString());
			intent.putExtra("CHAMPION", data.get("champion").toString());
			Log.d("타임어택",data.get("sentenceId").toString());
			Log.d("타임어택",data.get("sentence").toString());
			Log.d("타임어택",data.get("champion").toString());
			
			mContext.startActivity(intent);
			//mActivity.finish();
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	public int getTotalCount(){
		return this.totalCount;
	}
	public int getCurrentIndex(){
		return this.currentIndex;
	}
	
	@Override
	public void onSyncComplete(int resultCode, ServerComunicationDTO dto, Object resultObj) {
		if(resultCode == OnDataSyncCompleteListener.RESULT_SYNC_FAIL){
			if(resultObj!=null)
				ServerComunicationUtil.showNotify((String)resultObj, mContext);
		}else{
			if(dto.getActionType() == ServerComunicationUtil.TYPE_TIME_ATTACK_LIST){
				try{
					JSONObject resultObject = new JSONObject((String)resultObj);
					JSONArray resultArray = new JSONArray(resultObject.get("list").toString());
					for(int i = 0 ; i < resultArray.length(); i++){
						list.put(resultArray.get(i)); 
					}
					totalCount = resultObject.getInt("totalCount");
					currentIndex = currentIndex + rowCount;
					adapter.notifyDataSetChanged();
				}catch(Exception e){
					e.printStackTrace();
				}
			}		
		}
	}
	@Override
	public void onSyncComplete(int resultCode) {
		// TODO Auto-generated method stub
		
	}
}
